package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.*;
import com.rongke.service.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @GoodsQuerstionController
 * @体验众测问卷选择Controller
 * @version : Ver 1.0
 */
@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@RequestMapping(value="/api/goodsQuerstion")
@Transactional
public class GoodsQuerstionController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private GoodsQuerstionService goodsQuerstionService;


    @Autowired
    private QuestionnaireService questionnaireService;
    @Autowired
    private QuestionOptionService questionOptionService;
    @Autowired
    private UserQuestionService userQuestionService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private OrdersService ordersService;



    /**
     * @添加体验众测问卷选择
     * @param goodsQuerstion
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addGoodsQuerstion(@RequestBody GoodsQuerstion goodsQuerstion){
        log.debug("添加体验众测问卷选择");
        goodsQuerstionService.insert(goodsQuerstion);
        return JsonResp.ok(goodsQuerstion);
    }

    /**
     * @修改体验众测问卷选择
     * @param goodsQuerstion
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateGoodsQuerstion(@RequestBody GoodsQuerstion goodsQuerstion){
        log.debug("修改体验众测问卷选择");
        goodsQuerstionService.updateById(goodsQuerstion);
        return JsonResp.ok(goodsQuerstion);
    }
    /**
     * @根据id查找体验众测问卷选择
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectGoodsQuerstion(Long id){
        log.debug("查找体验众测问卷选择");
        GoodsQuerstion goodsQuerstion = goodsQuerstionService.selectById(id);
        return JsonResp.ok(goodsQuerstion);
    }

    /**
     * @根据条件查找体验众测问卷选择
     * @param goodsQuerstion
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectGoodsQuerstionByCondition(@RequestBody GoodsQuerstion goodsQuerstion){
        log.debug("根据条件查找体验众测问卷选择");
        if(goodsQuerstion==null)
            return JsonResp.toFail("参数不能为空");
        List<GoodsQuerstion> goodsQuerstionList=goodsQuerstionService.selectByCondition(goodsQuerstion);
        return JsonResp.ok(goodsQuerstionList);
    }
    /**
     * @删除
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/deleteExGoods", method = RequestMethod.GET)
    public JsonResp deleteExGoods(Long id){
        log.debug("删除");
        EntityWrapper<GoodsQuerstion> gq = new EntityWrapper<>();
        gq.eq("goods_id",id.toString());
        GoodsQuerstion goodsQuerstion = goodsQuerstionService.selectOne(gq);
        if(goodsQuerstion != null){
            EntityWrapper<GoodsQuerstion> ew = new EntityWrapper<>();
            ew.eq("goods_id",goodsQuerstion.getGoodsId());
            goodsQuerstionService.delete(ew);
            EntityWrapper<UserQuestion> uq = new EntityWrapper<>();
            uq.eq("goods_id",goodsQuerstion.getGoodsId());
            userQuestionService.delete(uq);
            return JsonResp.ok();
        }else{
            return JsonResp.toFail("id不匹配");
        }

    }




    /**
     * 查询该商品下的问卷
     * @param id
     * @return
     */
    @RequestMapping(value = "/getGoodsQuesList",method = RequestMethod.GET)
    public JsonResp getNotGroupedGoodsList(Long id){
        log.debug("查询该商品下的问卷");
        EntityWrapper<GoodsQuerstion> ew=new EntityWrapper<>();
        ew.eq("goods_id",id);
        GoodsQuerstion goodsQuerstion =goodsQuerstionService.selectOne(ew);
        if(goodsQuerstion!=null){
            Questionnaire questionnaire = questionnaireService.selectById(goodsQuerstion.getQuestionnaireId());
            /*String ids = "";
            for(int i = 0;i<goodsList.size();i++){
                ids +=  goodsList.get(i).getQuestionnaireId()+ ",";
            }*/
            EntityWrapper<Question> qu = new EntityWrapper<>();
            qu.in("questionnaire_id",goodsQuerstion.getQuestionnaireId().toString());
            List<Map<String,Object>> list = questionService.selectMaps(qu);
            Map<String,Object> maps = new HashMap<>();
            for (Map<String,Object> o:list) {
                EntityWrapper<QuestionOption> qot=new EntityWrapper<>();
                qot.in("question_id",o.get("id").toString());
                List<Map<String,Object>> oplist = questionOptionService.selectMaps(qot);
                for (Map<String,Object> m:oplist) {
                    //查询该问题下的所有人数
                    EntityWrapper<UserQuestion> uq = new EntityWrapper<>();
                    uq.eq("goods_id",id);
                    uq.eq("querstion_id",o.get("id").toString());
                    List<UserQuestion> uqlist = userQuestionService.selectList(uq);
                    //查询该问题下该选项的总数
                    EntityWrapper<UserQuestion> uqop = new EntityWrapper<>();
                    uqop.eq("option_id",m.get("id").toString());
                    List<UserQuestion> olist = userQuestionService.selectList(uqop);

                    double count = uqlist.size();//该问卷下总人数
                    double opsCount = olist.size();//该问卷下该选项的总人数
                    m.put("count",count);
                    m.put("opsCount",opsCount*100);
                }
                o.put("option",oplist);
            }
            maps.put("list",list);
            maps.put("naire",questionnaire);
            return JsonResp.ok(maps);
        }else{
            return JsonResp.toFail("该商品下无问卷数据");
        }
    }




    /**
     * @用户填写问卷
     * @param userQuestion
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/saveUserQues", method = RequestMethod.POST)
    public JsonResp saveUserQues(@RequestBody List<UserQuestion> userQuestion, Long orderId){
        log.debug("用户填写问卷");
        for (UserQuestion u: userQuestion) {
            u.setAddtime(new Date());
        }
        userQuestionService.insertBatch(userQuestion);
        Orders orders = new Orders();
        orders.setId(orderId);
        //设置订单状态为 4 已评价
        orders.setUptDatetime(new Date());
        orders.setCommentTime(new Date());
        orders.setOrderStatus(4);
        ordersService.updateById(orders);
        return JsonResp.ok(userQuestion);
    }


    /**
     * @选择问卷
     * @param questionnaireId
     * @param goodsId
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/goodsQuesAdd", method = RequestMethod.GET)
    public JsonResp updateQuestionnaire( Long questionnaireId, Long goodsId){
        log.debug("添加");
        EntityWrapper<GoodsQuerstion> ew = new EntityWrapper<>();
        ew.eq("goods_id",goodsId);
        List<GoodsQuerstion> list = goodsQuerstionService.selectList(ew);
        if(list.size()>0){
            goodsQuerstionService.delete(ew);
        }
        GoodsQuerstion goodsQuerstion=new GoodsQuerstion();
        goodsQuerstion.setGoodsId(goodsId);
        goodsQuerstion.setQuestionnaireId(questionnaireId);
        goodsQuerstionService.insert(goodsQuerstion);
        return JsonResp.ok(goodsQuerstion);
    }

}
